﻿module Chessboard

open Tile
open SI
open Math
open Casanova
open System.Collections.Generic
open System

 //[Serializable]
 type Chessboard = 
    {
        board : Tile array
    }

    static member Init(tileList : Tile array) = 
        let chessboard : Chessboard = 
            {
                board = Array.zeroCreate 49
            }

        let rand = Random()

        let c0 = tileList.[0]
        let c1 = tileList.[1]
        let c2 = tileList.[2]
        let c3 = tileList.[3]
        chessboard.board.[0] <- c0
        chessboard.board.[6] <- c1
        chessboard.board.[42] <- c2
        chessboard.board.[48] <- c3
        
        let tempList = new List<Tile>(tileList);
        tempList.RemoveAt(0);
        tempList.RemoveAt(0);
        tempList.RemoveAt(0);
        tempList.RemoveAt(0); 

        for i in 0..48 do
            if (tempList.Count <> 0) then
                if (i <> 0 && i <> 6 && i <> 42 && i <> 48) then
                    let index = rand.Next() % tempList.Count
                    let y = i % 7
                    let x = i / 7
                    let tile = tempList.Item(index)
                    chessboard.board.[i] <- tile
                    chessboard.board.[i].mPosition <- Vector2(float32(x) * 1.f<tile>, float32(y) * 1.f<tile>)
                    tempList.RemoveAt(index)
        let mutable remainingTile = tempList.Item(0)
        remainingTile.mPosition <- Vector2(-1.f<tile>, -1.f<tile>) 
        chessboard

    member this.GetNeighbors(iTile) = 
        let mutable neighbors = []
        let tilePos = iTile.mPosition
        if (0.f<tile> < tilePos.X && tilePos.X < 6.f<tile>) then
            let x = int( iTile.mPosition.X / 1.f<tile> )
            let y = int( iTile.mPosition.Y / 1.f<tile> )

            if ( iTile.IsTileConnected( this.board.[ (x - 1) * 7 + y ] ) ) then
                neighbors <- this.board.[ (x - 1) * 7 + y ] :: neighbors
            if (iTile.IsTileConnected( this.board.[ (x + 1) * 7 + y ] ) ) then
                neighbors <- this.board.[ (x + 1) * 7 + y ]  :: neighbors
        else if (0.f<tile> = tilePos.X) then
            let x = int( iTile.mPosition.X / 1.f<tile> )
            let y = int( iTile.mPosition.Y / 1.f<tile> )

            if (iTile.IsTileConnected( this.board.[ (x + 1) * 7 + y ] ) ) then
                neighbors <- this.board.[ (x + 1) * 7 + y ]  :: neighbors
        else
            let x = int( iTile.mPosition.X / 1.f<tile> )
            let y = int( iTile.mPosition.Y / 1.f<tile> )

            if ( iTile.IsTileConnected( this.board.[ (x - 1) * 7 + y ] ) ) then
                neighbors <- this.board.[ (x - 1) * 7 + y ] :: neighbors

        if (0.f<tile> < tilePos.Y && tilePos.Y < 6.f<tile>) then
            let x = int( iTile.mPosition.X / 1.f<tile> )
            let y = int( iTile.mPosition.Y / 1.f<tile> )

            if (iTile.IsTileConnected(this.board.[ x * 7 + y - 1 ] )) then
                neighbors <- this.board.[ x * 7 + y - 1] :: neighbors
            if (iTile.IsTileConnected(this.board.[ x * 7 + y + 1 ])) then
                neighbors <- this.board.[ x * 7 + y + 1] :: neighbors
         else if (0.f<tile> = tilePos.Y) then
            let x = int( iTile.mPosition.X / 1.f<tile> )
            let y = int( iTile.mPosition.Y / 1.f<tile> )
            if (iTile.IsTileConnected(this.board.[ x * 7 + y + 1 ])) then
                neighbors <- this.board.[ x * 7 + y + 1] :: neighbors
         else
            let x = int( iTile.mPosition.X / 1.f<tile> )
            let y = int( iTile.mPosition.Y / 1.f<tile> )
            if (iTile.IsTileConnected( this.board.[x * 7 + y - 1 ])) then
                neighbors <- this.board.[ x * 7 + y - 1 ] :: neighbors
        neighbors

    member this.MoveToCloseTile(iTile, iDirection) = 
        let mutable temp = iTile        
        let x = int( iTile.mPosition.X / 1.f<tile> )
        let y = int( iTile.mPosition.Y / 1.f<tile> )

        if iDirection = POSITION.Up then
            if (x <> 0) then
                temp <- this.board.[ (x - 1) * 7 + y ]
        else if iDirection =  POSITION.Down then
            if (x <> 6) then
                temp <- this.board.[ (x + 1) * 7 + y ]
        else if iDirection =  POSITION.Left then
            if (y <> 0) then
                temp <- this.board.[ x * 7 + y - 1 ]
        else if iDirection =  POSITION.Right then
            if (y <> 6) then
                temp <- this.board.[ x * 7 + y + 1 ]
        else
            temp <- iTile
        temp